<template>
  <dt-modal-edit ref="edit" title="添加/编辑承接人" :width="900" @ok="onOk" :labelWidth="130"></dt-modal-edit>
</template>

<script>
  import editOpt from '../biz/addReceiver.js'
  import apiMgr from '@/biz/caseReceiver/index'

  export default {
    data() {
      return {
        info: {
          buildingId: '',
          receiverMsg: '承接人信息',
          userId: '',
          clueType: [],
          outCity: '',
          outPhoneX: '',
          tospurLabel: '同策好房投放渠道',
          tcCity: '',
          myTcInPhoneX: '',
          baiduLabel: '百度地图投放渠道',
          bdCity: '',
          myBdInPhoneX: '',
          phone: '',
          tospurVisible: true,
          baiduVisible: true
        },
        callInTcPage: {
          total: 0,
          page: 1,
          rows: 10,
          pages: ''
        },
        callInBdPage: {
          total: 0,
          page: 1,
          rows: 10,
          pages: ''
        },
        callOutPage: {
          total: 0,
          page: 1,
          rows: 10,
          pages: ''
        },
        hfFlag: true,
        bdFlag: true
      }
    },

    methods: {
      onOk(data) {
        if (data.outPhoneX == data.myTcInPhoneX || data.outPhoneX == data.myBdInPhoneX) {
          this.$Message.error('呼出号与呼入号重复，请重新选择')
        } else if (data.myTcInPhoneX && data.myBdInPhoneX && data.myTcInPhoneX == data.myBdInPhoneX) {
          this.$Message.error('呼入号重复，请重新选择')
        } else {
          let clueTypeStr = data.clueType.length == 2 && data.clueType[0] != 4 ? '3' : data.clueType[0]
          let params = {
            buildingId: data.buildingId,
            city: data.outCity,
            myBdInPhoneX: data.myBdInPhoneX || '',
            myTcInPhoneX: data.myTcInPhoneX || '',
            outPhoneX: data.outPhoneX,
            userId: data.userId,
            clueType: clueTypeStr,
            phone: data.phone,
            createId: this.$dt.userInfo().userId,
            updateId: this.$dt.userInfo().userId
          }
          console.log(params)
          apiMgr.receiverSaveOrUpdate(params).then(() => {
            this.$emit('success')
            this.$refs.edit.hide()
            this.$Message.success('配置成功')
          })
        }
      },
      getInit(userInfo) {
        this.info.outCity = ''
        this.info.outPhoneX = ''
        this.info.tcCity = ''
        this.info.myTcInPhoneX = ''
        this.info.bdCity = ''
        this.info.myBdInPhoneX = ''
        apiMgr
          .getReceiverInfoByBId({ buildingId: this.info.buildingId, userId: userInfo.userId })
          .then((res) => {
            let clueType = []
            if (res) {
              this.info.tospurVisible = res.hfFlag == 1 && res.clueType != '4' ? true : false
              this.info.baiduVisible = res.bdFlag == 1 && res.clueType != '4' ? true : false
              this.hfFlag = res.hfFlag == 1 ? true : false
              this.bdFlag = res.bdFlag == 1 ? true : false
              clueType = res.clueType == 3 ? ['1', '2'] : [String(res.clueType)]
            }
            this.info = { ...this.info, ...res, clueType }
            this.$refs.edit.show(editOpt.options(this), this.info)
          })
      },
      updateOpt() {
        this.$refs.edit.update(editOpt.options(this), this.info)
      },
      show(data) {
        console.log(data, '****')
        this.info.buildingId = data.buildingId
        if (data.userId) {
          this.getInit(data)
        } else {
          this.info.tospurVisible = data.hfFlag == 1 ? true : false
          this.info.baiduVisible = data.bdFlag == 1 ? true : false
          this.hfFlag = data.hfFlag == 1 ? true : false
          this.bdFlag = data.bdFlag == 1 ? true : false
          this.$refs.edit.show(editOpt.options(this), this.info)
        }
      }
    }
  }
</script>

<style lang="scss" scoped></style>
